-- Lua Battle System - 日志系统单元测试

local TestFramework = require("src.utils.test_framework")
local Logger = require("src.utils.logger")

local function run_tests()
    local suite = TestFramework.create_suite("Logger Unit Tests")

    -- 测试套件设置
    local original_level = Logger.level

    -- Setup函数：保存原始状态并重置
    local function setup()
        Logger.level = "debug" -- 设置为最低级别以测试所有输出
    end

    -- Teardown函数：恢复原始状态
    local function teardown()
        Logger.level = original_level
    end

    suite:setup(setup)
    suite:teardown(teardown)

    -- 测试1：Logger初始化
    suite:add_test("Logger initialization", function()
        local test_config = { level = "warn" }
        Logger.init(test_config)
        TestFramework.Assert.equals("warn", Logger.level, "Logger should initialize with given config")
    end)

    -- 测试2：日志级别检查
    suite:add_test("Log level validation", function()
        Logger.level = "info"

        -- 测试不同级别的日志是否应该输出
        TestFramework.Assert.is_true(Logger._should_log("error"), "Error level should always log")
        TestFramework.Assert.is_true(Logger._should_log("warn"), "Warn level should log when level is info")
        TestFramework.Assert.is_true(Logger._should_log("info"), "Info level should log when level is info")
        TestFramework.Assert.is_false(Logger._should_log("debug"), "Debug level should not log when level is info")
    end)

    -- 测试3：消息格式化
    suite:add_test("Message formatting", function()
        local test_message = "Test log message"
        local formatted = Logger._format_message("info", test_message)

        -- 检查格式化后的消息包含时间戳、级别和消息内容
        TestFramework.Assert.is_type("string", formatted, "Formatted message should be a string")
        TestFramework.Assert.is_true(string.find(formatted, "INFO") ~= nil, "Formatted message should contain log level")
        TestFramework.Assert.is_true(string.find(formatted, test_message) ~= nil, "Formatted message should contain original message")
    end)

    -- 测试4：日志级别定义
    suite:add_test("Log level definitions", function()
        TestFramework.Assert.equals(1, Logger.levels.debug, "Debug level should be 1")
        TestFramework.Assert.equals(2, Logger.levels.info, "Info level should be 2")
        TestFramework.Assert.equals(3, Logger.levels.warn, "Warn level should be 3")
        TestFramework.Assert.equals(4, Logger.levels.error, "Error level should be 4")
    end)

    -- 测试5：各级别日志函数存在性
    suite:add_test("Log function availability", function()
        TestFramework.Assert.is_type("function", Logger.debug, "Logger.debug should be a function")
        TestFramework.Assert.is_type("function", Logger.info, "Logger.info should be a function")
        TestFramework.Assert.is_type("function", Logger.warn, "Logger.warn should be a function")
        TestFramework.Assert.is_type("function", Logger.error, "Logger.error should be a function")
    end)

    -- 测试6：无效日志级别处理
    suite:add_test("Invalid log level handling", function()
        local original_level = Logger.level
        Logger.level = "invalid_level"

        -- 无效级别应该不会崩溃，但可能有默认行为
        TestFramework.Assert.is_type("string", Logger.level, "Logger level should remain a string even when invalid")

        -- 恢复级别
        Logger.level = original_level
    end)

    -- 测试7：日志级别优先级
    suite:add_test("Log level priority", function()
        Logger.level = "error"

        TestFramework.Assert.is_true(Logger._should_log("error"), "Error should log when level is error")
        TestFramework.Assert.is_false(Logger._should_log("warn"), "Warn should not log when level is error")
        TestFramework.Assert.is_false(Logger._should_log("info"), "Info should not log when level is error")
        TestFramework.Assert.is_false(Logger._should_log("debug"), "Debug should not log when level is error")
    end)

    -- 运行测试套件
    suite:run()

    -- 返回测试结果
    return TestFramework.get_stats().failed == 0
end

-- 导出测试函数
return {
    run_tests = run_tests
}